import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class leetcode2 {
    public int search(int[] nums, int target) {
        int left=0;
        int right=nums.length-1;
        while(left<=right){
            int mid=(right-left)/2+left;
            if(nums[mid]==target){
                return mid;
            }else if(nums[mid]<target){
                left=mid+1;
            } else if (nums[mid]>target) {
                right=mid-1;
            }
        }
        return -1;
    }
    public int searchInsert(int[] nums, int target) {
        int n=nums.length;
        int left=0,right=n-1,num=n;
        while(right>=left){
            int mid=(right-left)/2+left;
            if(nums[mid]>=target){
                num=mid;
                right=mid-1;
            }else{
                left=mid+1;
            }
        }
        return num;
    }
    public int mySqrt(int x) {
        int left=0;
        int right=x;
        int t=-1;
        while(left<=right){
            int mid=(right-left)/2+left;
            if((long) mid*mid<=x){
                t=mid;
                left=mid+1;
            }else{
                right=mid-1;
            }
        }
        return t;
    }
    public boolean isPerfectSquare(int num) {
        int left=0;
        int right=num;
        while(left<=right){
            int mid=(right-left)/2+left;
            long num1=(long) mid*mid;
            if(num1<num){
                left=mid+1;
            } else if (num1>num) {
                right=mid-1;
            }else {
                return true;
            }
        }
        return false;
    }
    public static int missingNumber(int[] nums) {
        Arrays.sort(nums);
        int left=0;
        int right=nums.length-1;
        int t=nums.length;
        while(left<=right){
            int mid=(right-left)/2+left;
            if(mid==nums[mid]){
                left=mid+1;
            }else{
                t=mid;
                right=mid-1;
            }
        }
        return t;
    }
    public int[] intersection(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Set<Integer> set=new HashSet<>();
        for (int i = 0; i < nums2.length; i++) {
            if(binarySearch(nums1,nums2[i])){
                set.add(nums2[i]);
            }
        }
        int[] s=new int[set.size()];
        int k=0;
        for(Integer num:set){
            s[k++]=num;
        }
        return s;
    }
    public boolean binarySearch(int[] nums1,int t){
        int left=0;
        int right=nums1.length-1;
        while(left<=right){
            int mid=(right-left)/2+left;
            if(nums1[mid]==t){
                return true;
            } else if (nums1[mid]>t) {
                right=mid-1;
            }else{
                left=mid+1;
            }
        }
        return false;
    }
    public int arrangeCoins(int n) {
        int left = 1, right = n;
        while (left < right) {
            int mid = (right - left + 1) / 2 + left;
            if ((long) mid * (mid + 1) <= (long) 2 * n) {
                left = mid;
            } else {
                right = mid - 1;
            }
        }
        return left;
    }
    public char nextGreatestLetter(char[] letters, char target) {
        int length=letters.length;
        if (target > letters[length - 1]) {
            return letters[0];
        }
        int low=0;
        int heigth=length-1;
        while(low<=heigth){
            int mid=(heigth-low)/2+low;
            if(letters[mid]>target){
                heigth=mid;
            }else{
                low=mid+1;
            }
        }
        return letters[low];
    }
    public int[] fairCandySwap(int[] aliceSizes, int[] bobSizes) {
        int sumA = Arrays.stream(aliceSizes).sum();
        int sumB = Arrays.stream(bobSizes).sum();
        int delta = (sumA - sumB) / 2;
        Set<Integer> rec = new HashSet<Integer>();
        for (int num : aliceSizes) {
            rec.add(num);
        }
        int[] ans = new int[2];
        for (int y : bobSizes) {
            int x = y + delta;
            if (rec.contains(x)) {
                ans[0] = x;
                ans[1] = y;
                break;
            }
        }
        return ans;
    }
    public boolean checkIfExist(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                if(i!=j){
                    if(arr[i]==2*arr[j]){
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[] nums={0,2,3,4};
        missingNumber(nums);
    }
}
